import LevelControler from "./LevelControler";
import GameHandler, { GameState } from "./GameHandler";



export default class StepManager extends Laya.Script3D {
    private static m_Instance: StepManager;
    public CurScene: Laya.Scene3D;
    private gameobject: Laya.Sprite3D;

    public static m_LevelControler: LevelControler;


    static Instance() {
        return StepManager.m_Instance;
    }
    onAwake() {
        this.gameobject = this.owner as Laya.Sprite3D;
        this.CurScene = this.owner.parent as Laya.Scene3D;
        this.Init();

        StepManager.m_Instance = this;
    }
    onUpdate(){
        if(StepManager.m_LevelControler != null){
            if(GameHandler.State > GameState.Idle && GameHandler.State < GameState.Finished)
            {
                StepManager.m_LevelControler.OnUpdate();
            }
        }
    }

    private Init() {
        console.log("Init StepManager");
        this.EnterLevel(2);
    }

    public GetScene() {
        this.CurScene;
    }

    private EnterLevel(level:Laya){
        GameHandler.GameScene = this.CurScene;
        GameHandler.CurLevelNode = this.CurScene.getChildByName("Levels").getChildByName(level.toString()) as Laya.Sprite3D;
        let player: Laya.Sprite3D = GameHandler.CurLevelNode.getChildByName("Control").getChildByName("Player") as Laya.Sprite3D;
        let camera: Laya.Camera = GameHandler.CurLevelNode.getChildByName("Control").getChildByName("Camera") as Laya.Camera;
        StepManager.m_LevelControler = new LevelControler(player, camera);
        GameHandler.State = GameState.Being;
    }
}